<template>
	<view class="light">
		<block v-if="show">
			<view class="light-box">
				<view class="search-result-input">
					<input type="text" v-model="form.keyword" confirm-type="search" placeholder="请输入关键字搜索" placeholder-class="place" @confirm="search"/>
					<u-icon name="close-circle-fill" color="#A4A4A4" size="35" @click="clearKeyword" v-if="form.keyword"></u-icon>
				</view>
				<view class="search-result-function">
					<pt-small-function
						:isMerchantClass="true"
						:Multiple="true"
						:showClass="true"
						:styleID="form.style_id"
						:stylePopList="style_list"
						:spaceID="form.space_id"
						:spacePopList="space_list"
						:classID="form.class_id"
						:classPopList="class_list"
						:sortPopList="sort_list"
						:sortList="sortList"
						@clickItem="clickItem"
						@changeType="changeType">
					</pt-small-function>
				</view>
				<view class="search-goods-list">
					<block v-if="goods_list.length">
						<pt-product-items 
							:isBig="isBig" 
							:object="{goods_list: goods_list}" 
							:showSort="false"
							:showSaleCounts="true"
							:light="true"
							:isLogin="isLogin">
						</pt-product-items>
					</block>
					<pt-nothing
						text="暂无商品"
						:fixed="true"
						top="216"
						bgColor="#FFF"
						icon="https://jiajudashi.oss-cn-shenzhen.aliyuncs.com/images/shop/icons/nothing.png?v=1"
						v-if="noMore">
					</pt-nothing>
				</view>
			</view>
			<pt-loading-more v-if="isLoading"></pt-loading-more>
			<pt-nomore v-if="noMore && goods_list.length"></pt-nomore>
		</block>
		<pt-loading></pt-loading>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				Refresh: false,
				form: {
					keyword: '',
					page: 1,
					style_id: '',
					space_id: '',
					class_id: '',
					orderby: '',
					ascending: '',
					type_ids_: ''
				},
				style_list: [{
					style_id: '',
					style_name: '风格'
				}],
				space_list: [{
					space_id: '',
					space_name: '材质'
				}],
				class_list: [{
					class_id: '',
					class_name: '类型'
				}],
				sortList: [{
					name: '综合'
				},{
					name: '材质'
				},{
					name: '风格'
				},{
					name: '类型'
				}],
				sort_list: [{
					sort_id: '',
					selected: false,
					sort_name: '综合'
				},{
					sort_id: 5,
					selected: false,
					sort_name: '销量'
				},{
					sort_id: 6,
					selected: false,
					sort_name: '价格'
				}],
				goods_list: [],
				isBig: false,
				isLoading: false,
				noMore: false,
				// 足迹参数
				addFootParam: {
					start_time: '',
					end_time: '',
					stay_time: 0
				},
				isLogin: false
			};
		},
		onShow() {
			if(uni.getStorageSync('userInfo')){
				this.isLogin = true
			}else{
				this.isLogin = false
			}
		},
		onLoad(options) {
			if(options.keyword){
				this.form.keyword = options.keyword
			}
			// 如果用户从分享的小程序卡片点击进来的,设置一个url缓存,登录后返回当前缓存url
			let pages = getCurrentPages();
			let curPage = pages[pages.length - 1];
			uni.setStorageSync('newurl',curPage.$page.fullPath)
			this.init()
			this.getClass()
		},
		onPullDownRefresh() {
			this.goods_list = []
			this.style_list = [{
				style_id: '',
				style_name: '风格'
			}]
			this.space_list = [{
				space_id: '',
				space_name: '材质'
			}]
			this.class_list = [{
				class_id: '',
				class_name: '类型'
			}]
			this.noMore = false
			this.Refresh = true
			this.init()
			this.getClass()
		},
		onHide() {
			this.addFootParam.end_time = parseInt(new Date().getTime() / 1000)
			this.addFootParam.stay_time = this.addFootParam.end_time - this.addFootParam.start_time
			if(uni.getStorageSync('userInfo')){
				this.addFoot('查看灯饰')
			}
		},
		onUnload() {
			this.addFootParam.end_time = parseInt(new Date().getTime() / 1000)
			this.addFootParam.stay_time = this.addFootParam.end_time - this.addFootParam.start_time
			if(uni.getStorageSync('userInfo')){
				this.addFoot('查看灯饰')
			}
		},
		mounted() {
			this.addFootParam.start_time = parseInt(new Date().getTime() / 1000)
		},
		methods: {
			addFoot(where){
				this.$u.api.AddFoot({
					// #ifdef MP-WEIXIN
					type: 1,
					// #endif
					// #ifdef APP-PLUS
					type: 2,
					// #endif
					// #ifdef H5
					type: 3,
					// #endif
					store_id: uni.getStorageSync('userInfo')?JSON.parse(uni.getStorageSync('userInfo')).store_id:'',
					client: 1,
					where: where,
					stay_time: this.addFootParam.stay_time,
					goods_id: this.isMerchant?0:this.goods_id,
					store_goods_id: this.isMerchant?this.goods_id:0,
					is_new: 1
				})
			},
			clickItem(e){
				if(e.sort){
					if(e.sort!=='sort'){
						this.form.orderby = e.sort
					}
				}else{
					this.form.orderby = ''
				}
				this.form.type_ids_ = ''
				if(e.asc){
					if(e.asc==1){
						this.form.ascending = 'ASC'
					}else{
						this.form.ascending = 'DESC'
					}
				}else{
					this.form.ascending = ''
				}
				
				// 商品筛选组合拼接
				if(e.space_id && e.style_id && e.class_id){
					this.form.type_ids_ += e.space_id + ',' + e.style_id + ',' + e.class_id
				}else if(e.space_id && !e.style_id && !e.class_id){
					this.form.type_ids_ += e.space_id
				}else if(e.space_id && e.style_id && !e.class_id){
					this.form.type_ids_ += e.space_id + ',' + e.style_id
				}else if(e.space_id && !e.style_id && e.class_id){
					this.form.type_ids_ += e.space_id + ',' + e.class_id
				}else if(!e.space_id && !e.style_id && e.class_id){
					this.form.type_ids_ += e.class_id
				}else if(!e.space_id && e.style_id && !e.class_id){
					this.form.type_ids_ += e.style_id
				}
				this.goods_list = []
				this.form.page = 1
				this.noMore = false
				this.init()
			},
			changeType(e){
				if(e==1){
					this.isBig = true
				}else{
					this.isBig = false
				}
			},
			init(){
				this.$u.get('https://www.deng51.com/api/v1/buyer/goods/noDecode/searchList',{
					keyword_: this.form.keyword,
					type_ids_: this.form.type_ids_,
					currentPage: this.form.page,
					orderby: this.form.orderby,
					ascending: this.form.ascending
				}).then(res => {
					this.show = true
					this.isLoading = false
					if(res.data.pdList.length){
						res.data.pdList.forEach(item => {
							this.goods_list.push({
								image: item.main_img_,
								goods_id: item.id_,
								goods_name: item.name_,
								retail_price: (item.cost_ * 2.5).toFixed(0),
								sale_count: item.sale_count_?item.sale_count_:0
							})
						})
					}else{
						this.noMore = true
					}
				}).then(() => {
					if(this.Refresh){
						setTimeout(() => {
							uni.showToast({
								icon: 'none',
								title: '刷新成功'
							})
							this.Refresh = false
							uni.stopPullDownRefresh()
						},1500)
					}
				})
			},
			clearKeyword(){
				this.form.keyword = ''
				uni.redirectTo({
					url: '/pages/light/light'
				})
			},
			getClass(){
				this.$u.get('https://www.deng51.com/api/v1/buyer/goods/noDecode/getGoodsTypeList').then(res => {
					if(res.data.length){
						res.data.forEach(item => {
							if(item.name_==='风格'){
								item.styleTypeDtoList.forEach(data => {
									data.subList.forEach(styleItem => {
										this.style_list.push({
											style_name: styleItem.name_,
											style_id: styleItem.id_
										})
									})
								})
							}else if(item.name_==='材质'){
								item.subList.forEach(data => {
									this.space_list.push({
										space_name: data.name_,
										space_id: data.id_
									})
								})
							}else if(item.name_==='类型'){
								item.subList.forEach(data => {
									this.class_list.push({
										class_name: data.name_,
										class_id: data.id_
									})
								})
							}
						})
					}
				})
			},
			search(){
				this.goods_list = []
				this.form.page = 1
				this.init()
			}
		},
		onReachBottom() {
			if(!this.noMore){
				this.form.page++
				this.isLoading = true
				this.init()
			}
		},
	}
</script>

<style lang="scss">
	.light{
		.light-box{
			padding: 30rpx 30rpx 0;
		}
		.search-result-input{
			border-bottom: solid 5rpx #141414;
			padding-bottom: 20rpx;
			display: flex;
			align-items: center;
			input{
				flex: 1;
				min-width: 0;
				font-size: 32rpx;
			}
			.place{
				color: #A4A4A4;
			}
		}
	}
</style>
